/* model */
import { BaseSelectUserVuexPropertyEnum } from '@src/component/common/BaseSelectUser/store'
import { SelectUserComponentRole } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* util */
import { isEmpty, isNotEmpty } from '@src/util/type'
/* type */
import { 
  BaseSelectUserGetRoleListModel, 
  BaseSelectUserGetRoleListResult, 
  BaseSelectUserGetRoleUserListModel, 
  BaseSelectUserGetRoleUserListResult 
} from '@src/component/common/BaseSelectUser/types'

/** 
 * @description 选人控件 角色列表
*/
function useBaseSelectUserStoreRoleList() {
  
  const store = useBaseSelectUserStore()
  
  // 角色列表
  const roleList = computed(() => store.state.roleList)
  
  // 角色列表是否为空
  const isRoleListEmpty = computed(() => {
    return isEmpty(roleList.value)
  })
  
  // 角色列表是否不为空
  const isRoleListNotEmpty = computed(() => {
    return isNotEmpty(roleList.value)
  })
  
  const setRoleList = (value: SelectUserComponentRole[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetRoleList, value)
  }
  
  return {
    isRoleListEmpty,
    isRoleListNotEmpty,
    roleList,
    setRoleList
  }
}

/** 
 * @description 选人控件 当前查看展开的角色
*/
function useBaseSelectUserStoreCurrentToggleRole() {
  
  const store = useBaseSelectUserStore()
  
  // 当前查看展开的角色
  const currentToggleRole = computed(() => store.state.currentToggleRole)
  
  // 当前查看展开的角色是否为空
  const isCurrentToggleRoleNotEmpty = computed(() => {
    return isNotEmpty(currentToggleRole.value)
  })
  
  // 当前查看展开的角色是否不为空
  const isCurrentToggleRoleEmpty = computed(() => {
    return isEmpty(currentToggleRole.value)
  })
  
  const setCurrentToggleRole = (value: SelectUserComponentRole | null = null) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentToggleRole, value)
  }
  
  return {
    currentToggleRole,
    isCurrentToggleRoleEmpty,
    isCurrentToggleRoleNotEmpty,
    setCurrentToggleRole
  }
}

/** 
 * @description 查询选人控件的角色列表
*/
function useBaseSelectUserStoreFetchRoleList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchRoleList(params: BaseSelectUserGetRoleListModel): Promise<MsgModel<BaseSelectUserGetRoleListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchRoleList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchRoleList
}

/** 
 * @description 查询选人控件 角色下 人员列表
*/
function useBaseSelectUserStoreFetchRoleUserList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchRoleUserList(params: BaseSelectUserGetRoleUserListModel): Promise<MsgModel<BaseSelectUserGetRoleUserListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchRoleUserList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchRoleUserList
}

export {
  useBaseSelectUserStoreRoleList,
  useBaseSelectUserStoreCurrentToggleRole,
  useBaseSelectUserStoreFetchRoleList,
  useBaseSelectUserStoreFetchRoleUserList
}
